package services.xtwh;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import vo.system.AZ02;
import vo.system.AZ09;
import com.Const;
import com.PublicSystem;
import com.util.Util;
/**
* PersonMgrFacade:照片上传services
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
public class ZpscServices {
	PublicSystem sys = PublicSystem.getInstance();
	/**
	 * 下拉框选项
	 * @param lx
	 * @return
	 * @throws Exception
	 */
	public List getSelect(String lx) throws Exception {
		String sql = "select aaa103, aaa102 FROM aa10 where aaa100 = ?";
		List data = sys.query(sql, new Object[] {lx}, true);
		return data;
	}
	/**
	 * 查询个人制卡信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public HashMap getgrUnitData(String id) throws Exception {
//		String sql = "select AAC001, AAC003, AAZ501, AAC004, AAC005, AAC006, AAC002 from az20 t where t.AAE008 is null and t.aac001 = ? "
//				+" and t.AAE036 = (select max(AAE036) from az20 where aac001 = ?) ";
		String sql="select AAC001 from az20 t where aac001=? and aae008 is not null";
		String sql1 = "select aac100 from az02 where AAC001 = '" + id + "'";       
		String path = Const.server_path + "/Picture/";
		String name = id + "." + "JPG";
		String file = path + name;
		File files = new File(path);
		if(!files.exists()) { 
			files.mkdirs();
		}	
		List data = sys.query(sql, new Object[] {id}, true);	
		if(data.size() <= 0 ) {
		   sys.SelResultBlob(sql1, file);
		   String sql2="select  (select distinct aac001 from az02 t where t.aac001=a.aac001) as aac001 ,aac002,aac006,aac005,aac004,aac003 from ac01 a where a.aac001=?";
		   List data1 = sys.query(sql2, new Object[] {id}, true);
		   return data1.size() > 0 ? (HashMap)data1.get(0) : null;
		}else{
			return data.size() > 0 ? (HashMap)data.get(0) : null;
		}
		
	}
	/**
	 * 删除照片信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String deletedata(String id) throws Exception {
		AZ02 vo = new AZ02();	
	 	AZ09 voo = new AZ09();
	 	HashMap map = new HashMap();
	 	map.put("aac001", id);
	 	vo = (AZ02)Util.MapToVo(vo, map);
		voo = (AZ09)Util.MapToVo(voo, map);
	 	sys.BeginTransaction();
	 	sys.Jdbc_deleteDaoTransaction(vo, new Object[] {"aac001:A"});
		sys.Jdbc_deleteDaoTransaction(voo, new Object[] {"aac001:A"});
	 	sys.endTransaction();
	 	String res = check(id);
	 	if(res.equals("1")) {
	 		return "1";
	 	} else {
	 		return "0";
	 	}	  
	}
	/**
	 * 查询照片信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String check(String id) throws Exception {		
		String checksql = "select aac001 from az02 where aac001 ='" + id + "'";
		String res = sys.SelResultStr(checksql);
		if(null == res) {
			return "0";
		} else {
			return "1";
		}		
	}
}
